package com.youku.plugins.searchBarV4.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.PluginEventDispather;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.utils.BaseUtil;
   import com.youku.conductor.ConductorState;
   
   public class SearchBarMediator extends Object
   {
      
      public function SearchBarMediator(param1:MovieClip, param2:IPlayerProxy, param3:PluginEventDispather) {
         super();
         this._mainMC = param1;
         this._playerProxy = param2;
         this._pluginEventDispather = param3;
         this.init();
      }
      
      private var _mainMC:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _pluginEventDispather:PluginEventDispather;
      
      private var _searchBar:SearchBar;
      
      private var _firstShow:Boolean = true;
      
      private function init() : void {
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SEARCHBAR_RETAIN,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_START_TO_PLAY,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.PANEL_STATE_CHANGE,this.onPlayerEvent);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMediatorUpdata);
         this._searchBar = new SearchBar(this._playerProxy,this._pluginEventDispather);
      }
      
      private function onMediatorUpdata(param1:ConductorEvent) : void {
         var _loc2_:String = this._playerProxy.playerData.videoTitle;
         _loc2_ = _loc2_ + ("(" + BaseUtil.formatTime(this._playerProxy.playerData.videoTotalTime) + ")");
         this._searchBar.text = _loc2_;
      }
      
      private function onStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         this._searchBar.adFlag = false;
         switch(_loc2_)
         {
            case ConductorState.AD:
               this._searchBar.adFlag = true;
               break;
            case ConductorState.VIDEO:
               if(this._firstShow)
               {
                  this._firstShow = false;
                  this.show();
                  this._searchBar.retain = false;
               }
               break;
            case ConductorState.VIDEO_END:
               this._firstShow = true;
               break;
         }
      }
      
      private function onPlayerEvent(param1:PlayerControlEvent) : void {
         switch(param1.type)
         {
            case PlayerControlEvent.PANEL_STATE_CHANGE:
               this._searchBar.adFlag = param1.data.havePanel;
               break;
            case PlayerControlEvent.COMMON_RESET:
               this._firstShow = true;
               break;
            case PlayerControlEvent.COMMON_START_TO_PLAY:
               break;
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
               if(this._playerProxy.playerData.isInFullScreen)
               {
                  this._searchBar.visible = false;
               }
               else if(param1.data.width < 281 || param1.data.height < 221)
               {
                  this._searchBar.visible = false;
               }
               else
               {
                  this._searchBar.visible = true;
               }
               
               this._searchBar.changeSize(param1.data.width,param1.data.height);
               break;
            case PlayerControlEvent.SEARCHBAR_RETAIN:
               this.hide();
               this.show();
               this._searchBar.show(true);
               break;
         }
      }
      
      private function get shouldShowSearchBar() : Boolean {
         if(this._playerProxy.rootGetData.rootWinType == "popup" || this._playerProxy.rootGetData.rootWinType == "adshow" || this._playerProxy.rootGetData.rootWinType == "index")
         {
            return false;
         }
         var _loc1_:* = true;
         if(this._playerProxy.rootGetData.rootShowSearch)
         {
            _loc1_ = true;
         }
         else
         {
            _loc1_ = false;
         }
         return _loc1_;
      }
      
      private function show() : void {
         if(this.shouldShowSearchBar)
         {
            this._mainMC.addChild(this._searchBar);
         }
      }
      
      private function hide() : void {
         if(this._mainMC.contains(this._searchBar))
         {
            this._mainMC.removeChild(this._searchBar);
         }
      }
   }
}
